package com.mingzhi.lotterysystem.dao.mapper;

import com.mingzhi.lotterysystem.dao.dataobject.Encrypt;
import com.mingzhi.lotterysystem.dao.dataobject.UserDO;
import com.mingzhi.lotterysystem.service.enums.UserIdentityEnum;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select count(*) from user where email = #{email}")
    int countByMail(@Param("email") String email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * @param userDO
     * @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
     * 用于配置 MyBatis 在插入数据时获取数据库自动生成的主键。
     * 启用这个选项之后，MyBatis 会自动获取数据库插入操作后生成的主键值，并将其填充到相应的对象属性中。
     * useGeneratedKeys = true：启用自动获取生成的主键。
     * keyProperty = "id"：指定将生成的主键赋值给 id 属性。 "id" 指的是你的 Java 对象中的 id 属性。
     * keyColumn = "id"：指定数据库表中主键列的名字。
     */
    @Insert("insert into user(user_name,email,phone_number,password,identity)" +
            " values(#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("select * from user where email = #{email}")
    UserDO selectByMail(@Param("email") String email);

    @Select("<script>" +
            " select * from user" +
            " <if test =\"identity!=null\">" +
            " where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    /**
     * 根据前端传的id在表中进行判断
     *
     * @param ids
     * @return
     */
    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item = 'item' collection = 'items' open = '(' separator = ',' close = ')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);
}
